package com.ryder.book_cycle.controller;

import com.ryder.book_cycle.common.response.Result;
import com.ryder.book_cycle.entity.MerchantProfile;
import com.ryder.book_cycle.service.MerchantProfileService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Objects;
import java.util.Map;

/**
 * 描述: 商家店铺信息表(MerchantProfile)表控制层
 * 
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-07-09 04:00:16
 */
@RestController
@RequestMapping("/merchantProfile")
public class MerchantProfileController {

    private static final Logger log = LoggerFactory.getLogger(MerchantProfileController.class);

    @Autowired
    private MerchantProfileService merchantProfileService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result<Page<MerchantProfile>> pageList(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            MerchantProfile queryParam) {
        try {
            Page<MerchantProfile> page = new Page<>(pageNum, pageSize);
            QueryWrapper<MerchantProfile> wrapper = new QueryWrapper<>(queryParam);
            Page<MerchantProfile> result = merchantProfileService.page(page, wrapper);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("分页查询失败：{}", e.getMessage(), e);
            return Result.fail("分页异常");
        }
    }

    /**
     * 根据主键查询
     */
    @GetMapping("/{id}")
    public Result<MerchantProfile> queryById(@PathVariable Long id) {
        try {
            MerchantProfile entity = merchantProfileService.getById(id);
            return entity != null ? Result.ok(entity) : Result.fail("未找到数据");
        } catch (Exception e) {
            log.error("查询失败：{}", e.getMessage(), e);
            return Result.fail("查询异常");
        }
    }

    /**
     * 新增
     */
    @PostMapping
    public Result<Boolean> add(@RequestBody MerchantProfile entity) {
        try {
            boolean saved = merchantProfileService.save(entity);
            return saved ? Result.ok(true) : Result.fail("新增失败");
        } catch (Exception e) {
            log.error("新增失败：{}", e.getMessage(), e);
            return Result.fail("新增异常");
        }
    }

    /**
     * 修改
     */
    @PutMapping
    public Result<Boolean> update(@RequestBody MerchantProfile entity) {
        try {
            boolean updated = merchantProfileService.updateByMerchantId(entity);
            return updated ? Result.ok(true) : Result.fail("更新失败");
        } catch (Exception e) {
            log.error("更新失败：{}", e.getMessage(), e);
            return Result.fail("更新异常");
        }
    }

    /**
     * 删除
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        try {
            boolean removed = merchantProfileService.removeById(id);
            return removed ? Result.ok(true) : Result.fail("删除失败");
        } catch (Exception e) {
            log.error("删除失败：{}", e.getMessage(), e);
            return Result.fail("删除异常");
        }
    }

}
